package com.sdp.wx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.on_file.entity.OnFile;
import com.sdp.wx.db.on_file.service.IOnFileService;
import com.sdp.wx.db.wx_ipc_menu_list.entity.WxIpcMenuList;
import com.sdp.wx.db.wx_ipc_menu_list.service.IWxIpcMenuListService;
import com.sdp.wx.utils.LocalDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-03-11
 */
@RestController
@RequestMapping("/WxIpcMenuListController")
public class WxIpcMenuListController {
    @Autowired
    private IWxIpcMenuListService iWxIpcMenuListService;
    private String onlyofficeFiles = "onlyofficeFiles";
    @Autowired
    private IOnFileService iOnFileService;

    @Value("${sdp.filePath}")
    private String filePath;
    @Autowired
    private Query query;

    @PostMapping(value = "/page")
    public Result<?> page(@RequestBody DataCenter dataCenter) {
        return Result.ok(query.page(iWxIpcMenuListService, WxIpcMenuList.class, dataCenter));
    }
    @PostMapping(value = "/save")
    @Transactional(rollbackFor=Exception.class)
    public Result<?> save(@RequestBody WxIpcMenuList wxIpcMenuList) {
        if(wxIpcMenuList == null) {
            return Result.error("提交信息为空");
        }
        if(StringUtils.isEmpty(wxIpcMenuList.getKeyId())) {
            wxIpcMenuList.setCreateTime(LocalDateTime.now());
            iWxIpcMenuListService.save(wxIpcMenuList);
        } else {
            iWxIpcMenuListService.updateById(wxIpcMenuList);
        }
        return Result.ok(wxIpcMenuList);
    }
    @GetMapping(value = "/del")
    @Transactional(rollbackFor=Exception.class)
    public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
        if(StringUtils.isEmpty(keyId)) {
            return Result.error("需要删除的记录信息不完整");
        }
        iWxIpcMenuListService.removeById(keyId);
        return Result.ok();
    }
    @GetMapping(value = "/saveWord")
    public Result<?> getWord(@RequestParam(name="keyId",required=true) String keyId) throws IOException {
        QueryWrapper<OnFile> onFileQueryWrapper=new QueryWrapper<>();
        onFileQueryWrapper.lambda().eq(OnFile::getFileId,keyId);
        OnFile onFile=  iOnFileService.getOne(onFileQueryWrapper);
        if(onFile == null){
            String getLocalDate= LocalDateUtil.getLocalDate();
            String dbpath=onlyofficeFiles+"/"+getLocalDate+".docx";
            String filePath =this.filePath+"/"+onlyofficeFiles;
            // 创建文件
            File target = new File(filePath);
            if (!target.exists()) {
                target.mkdirs();
            }
            String office =filePath+"/"+getLocalDate+".docx";
            File file = new File(office);
            file.createNewFile();
            onFile=new OnFile();
            onFile.setFileName("");
            onFile.setFileType("docx");
            onFile.setFileSite(dbpath);
            onFile.setFileId(keyId);
            onFile.setVersion(getLocalDate);
            onFile.setCreateTime(LocalDateTime.now());
            iOnFileService.save(onFile);
        }
        return Result.ok(onFile);
    }
}

